//
//  LJFDetailtopView.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/20.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFDetailtopView.h"
#import "UIImageView+AFNetworking.h"

@implementation LJFDetailtopView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

//- (void)awakeFromNib
//{
//   CGFloat width = [[UIScreen mainScreen] bounds].size.width;
//    
//   
//}

- (void)setDataDict:(NSDictionary *)dataDict
{
    _dataDict = dataDict;
    
    [self loadScroll];
    [self loadButtonScroll];
    [self updateTopViewData];
}


- (void)updateTopViewData
{
    _nameLabel.text = _dataDict[@"goods_name"];
    _alreadyShop.text = _dataDict[@"goods_sales_count"];
    _shopPriceLabel.text = _dataDict[@"shop_price"];
    _smarkPriceLabel.text = [NSString stringWithFormat:@"市场价:%@",_dataDict[@"market_price"]];
    
    NSNumber *rankNum = _dataDict[@"comment_rank1"];
    _scoreLabel.text = [NSString stringWithFormat:@"%@(%@)",[rankNum stringValue],_dataDict[@"comment_rank_txt"]];
    
    NSNumber *starNum = _dataDict[@"comment_rank"];
    _hiddenViewW.constant = (120*[starNum floatValue])/5.0;
    
    if ([_dataDict[@"app_cut_price_desc"] length] == 0)
    {
        _discountConstant.constant = 8;
        _discountImg.hidden = YES;
        _discountLabel.hidden = YES;
    }
}

- (void)loadScroll
{
    NSArray *arr = _dataDict[@"pictures"];
    
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    
    _ScrollView.contentSize = CGSizeMake(width*arr.count, 0);
    
    _ScrollView.pagingEnabled = YES;
    _ScrollView.delegate = self;
    _ScrollView.tag = 176;
    
    [self addSubview:_ScrollView];
    
    for (int i = 0; i < arr.count; i++)
    {
        UIImageView *imgview=[[UIImageView alloc] initWithFrame:CGRectMake(i*width, 0, width, 200)];
        
        NSDictionary *dict = arr[i];
        
        [imgview setImageWithURL:[NSURL URLWithString:dict[@"thumb"]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        
        [_ScrollView addSubview:imgview];
        
    }
    
    [self loadPageControllerWithCount:arr.count];
}

- (void)loadButtonScroll
{
    CGFloat buttonWidth = self.frame.size.width/5.0;
    
    _greenWith.constant = buttonWidth;
    
    NSArray *arr = @[@"服务详情",@"服务介绍",@"客户点评",@"先睹为快",@"相关服务"];
    
    for (int i = 0; i < arr.count; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*buttonWidth, 390, buttonWidth, 30);
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.tag = 170 + i;
        button.titleLabel.adjustsFontSizeToFitWidth = YES;
        
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
    }
}

- (void)buttonClick:(UIButton *)button
{
    NSInteger tag = button.tag - 170;

    _greenX.constant = tag*button.frame.size.width;
    
    [_delegate DetailtopViewOffsetCollection:self andoffset:tag];
}


- (void)loadPageControllerWithCount:(NSInteger)count
{
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    
    CGFloat pageWidth = 20*count;
    CGFloat pageX = (width - pageWidth)/2.0;
    
    _page=[[UIPageControl alloc] initWithFrame:CGRectMake(pageX, 170, pageWidth, 30)];
    
    _page.numberOfPages=count;
    
    _page.pageIndicatorTintColor=[UIColor whiteColor];
    _page.currentPageIndicatorTintColor=[UIColor greenColor];
    
    [self addSubview:_page];

    [self Starttime];
}

- (void)Starttime
{
    _time = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(go) userInfo:nil repeats:YES];
    
    NSRunLoop *loop=[NSRunLoop currentRunLoop];
    
    [loop addTimer:_time forMode:NSRunLoopCommonModes];
}

- (void)go
{
    _index ++;
    
    NSArray *arr = _dataDict[@"pictures"];
    if (_index >= arr.count)
    {
        _index = 0;
    }
    
    [_ScrollView setContentOffset:CGPointMake(self.frame.size.width*_index, 0) animated:YES];
    _page.currentPage = _index;
}

- (void)stopTime
{
    if ([_time isValid])
    {
        [_time invalidate];
        _time = nil;
    }
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollView.tag == 176)
    {
        [self stopTime];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (scrollView.tag == 176)
    {
        [self Starttime];
    }
}




@end
